﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    class JLTAB55Report : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        public string KRS, LFD, POS;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string strExcelFile;
        Dictionary<string, int> qpplan = new Dictionary<string, int>();

        #endregion
        public JLTAB55Report()
            : base()
        {
        }
        public JLTAB55Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            if (dicParamater.ContainsKey("ROOTDRIVE"))
            {
                Root = dicParamater["ROOTDRIVE"].ToString();
            }
            else
            {
                Root = "C:\\IntexReport";
            }


        }
        #region------------------------------Function------------------------
        public void FillInExcelData(DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            /* WVORDER	ARTICLE	CO	ORDQTY	ALLQTY	ABPANZSTK	PQTY	ABPSTARTDAT	ABPENDDAT	
             * FDDIK	FDDIS	DES	STR	YARN	BEAM	MACHINE*/
            try
            {               
                DataTable dtSoruce = setSoruce.Tables["AB55"];
                DataRow temp = dtSoruce.Rows[0];
                string WVORDER = temp["WVORDER"].ToString().Trim();
                exAgent.SetCellValue(6, 20, WVORDER);
                string ARTICLE = temp["ARTICLE"].ToString().Trim();
                exAgent.SetCellValue(6, 1, ARTICLE);
                string DES = temp["DES"].ToString().Trim();
                exAgent.SetCellValue(6, 8, DES);
                string MACHINE = temp["MACHINE"].ToString().Trim();
                exAgent.SetCellValue(6, 28, MACHINE);
                string FDD = temp["FDDIK"].ToString().Trim() + "*" + temp["FDDIS"].ToString().Trim().TrimEnd('0');
                exAgent.SetCellValue(8, 1, FDD);
                string STR = temp["STR"].ToString().Trim();
                exAgent.SetCellValue(8, 13, STR);
                string YARN = temp["YARN"].ToString().Trim();
                exAgent.SetCellValue(8, 20, YARN);
                string ABPANZSTK = temp["ABPANZSTK"].ToString().Trim();
                exAgent.SetCellValue(10, 1, ABPANZSTK);
                string PQTY = temp["PQTY"].ToString().Trim();
                exAgent.SetCellValue(10, 8, PQTY);
                string ORDQTY = temp["ORDQTY"].ToString().Trim();
                exAgent.SetCellValue(10, 13, ORDQTY);
                string ABPSTARTDAT = temp["ABPSTARTDAT"].ToString().Trim().Substring(0, 4) + "-" +
                    temp["ABPSTARTDAT"].ToString().Trim().Substring(4, 2) + "-" +
                    temp["ABPSTARTDAT"].ToString().Trim().Substring(6, 2);
                exAgent.SetCellValue(10, 20, ABPSTARTDAT);
                string ABPENDDAT = temp["ABPENDDAT"].ToString().Trim().Substring(0, 4) + "-" +
                    temp["ABPENDDAT"].ToString().Trim().Substring(4, 2) + "-" +
                    temp["ABPENDDAT"].ToString().Trim().Substring(6, 2);
                exAgent.SetCellValue(10, 28, ABPENDDAT);
                string BEAM = temp["BEAM"].ToString().Trim();
                exAgent.SetCellValue(12, 1, BEAM);
                string ALLQTY = temp["ALLQTY"].ToString().Trim();
                exAgent.SetCellValue(12, 8, ALLQTY);
                //string DAT = System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-" + System.DateTime.Now.Day;
                //exAgent.SetCellValue(45, 26, DAT);        
                string REMARK = temp["FPATTERN"].ToString()+Environment.NewLine;
                if (REMARK==null)
                {
                    REMARK = "A1\r\n";
                }
                REMARK = REMARK.Replace(")", ")*");
                string REMARK1 = "";
                DataTable dtS = setSoruce.Tables["TT"];
                foreach (DataRow dw in dtS.Rows)
                {
                    REMARK1 = REMARK1 + dw["REMARK"].ToString().Trim() + Environment.NewLine;
                }

                exAgent.SetCellValue(20, 1, REMARK1);
                DataTable dtSoruce1 = setSoruce.Tables["ABPOS"];
                
                DataRow temp1 = dtSoruce1.Rows[0];
                //DataRow temp1 = dtSoruce1.Rows[0];
                //string ABPLIEFDATN = temp["ABPLIEFDATN"].ToString().Trim();
                string abpart = temp1["ABPART"].ToString().Trim();
                exAgent.SetCellValue(1, 18, abpart);
                string ABPLIEFDATN = "";
                string ABPSOLLMENGE8 = "";
                string ABPSOLLMENGE1 = "";
                string ABAKDPARTIE = "";
                ABAKDPARTIE = temp1["ABAKDPARTIE"].ToString().Trim();
                ABAKDPARTIE = Helpers.Helper.GetGB2312Value(ABAKDPARTIE).Trim();
                ABPLIEFDATN = temp1["ABPSTARTDAT"].ToString().Trim();
                ABPSOLLMENGE1 = temp1["ABPSOLLMENGE1"].ToString().Trim();
                if (REMARK=="A1\r\n")
                {
                     REMARK = REMARK.Replace("A", "A" + ABAKDPARTIE);
                }             
                REMARK = REMARK + "/总数" + ABPSOLLMENGE1;
                exAgent.SetCellValue(15, 1, REMARK);
                ABPSOLLMENGE8 = temp1["ABPSOLLMENGE8"].ToString().Trim();
                ABPLIEFDATN = ABPLIEFDATN.Substring(0, 4) + "-" + ABPLIEFDATN.Substring(4, 2) + "-" + ABPLIEFDATN.Substring(6, 2);

                    //temp1["ABPSTARTDAT"].ToString().Trim().Substring(4, 2) + "-" +
                    //temp1["ABPSTARTDAT"].ToString().Trim().Substring(6, 2);
                exAgent.SetCellValue(12, 28, ABPLIEFDATN);

                //ABPSOLLMENGE8 = temp1["ABPSOLLMENGE8"].ToString().Trim();
                ABPSOLLMENGE8 = (Convert.ToDouble(ABPSOLLMENGE8) * 0.1 - 100).ToString();
                ABPSOLLMENGE8 =ABPSOLLMENGE8+ "%";
                exAgent.SetCellValue(8, 8, ABPSOLLMENGE8);

                

                //DataTable dtrSoruce = setSoruce.Tables["WB"];
                //string WBBSTKNR = "", QTY = "";
                //int i = 21;
                //foreach (DataRow dw in dtrSoruce.Rows)
                //{
                //    WBBSTKNR = dw["WBBSTKNR"].ToString().Trim();
                //    exAgent.SetCellValue(i, 1, WBBSTKNR);
                //    QTY = dw["PQTY"].ToString().Trim();
                //    exAgent.SetCellValue(i, 7, QTY);
                //    i = i + 1;
                //}


            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = false;
            try
            {
                strCompany = "JLT";
                strExcelFile = @"JLT\JLTAB55DOC.xls";
                Standard = "AB55D";
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\JLT\JLTAB55DOC.xls");
                destfolder = SetFolder(Root, "织造");
                string filedate = KRS + "-" + LFD + "-" + POS;
                string newfilename = @"织造上机单" + filedate;

                exAgent.CheckWorkBook(newfilename);
                ServerShareFile(IsFTP, year, month, newfilename, fileType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void ServerShareFile(bool IsFTP, string year, string month, string ordernumer, string fileType)
        {
            string destfile, tempfile = "";
            string fileabsname = ordernumer;
            try
            {
                filename = destfolder + "\\" + fileabsname + ".xls"; ;
                destfile = filename;
                tempfile = CreateFile(ordernumer, IsFTP);
                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                File.Move(tempfile, destfile);

                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
        private string CreateFile(string orderno, bool IsFTP)
        {
            string filename = "";
            try
            {
                string pwd = "";
                string sheetName = orderno;
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                exAgent.CreateNewSheet(orderno + ".xls", Standard, sheetName, pwd);
                filename = exAgent.FileName;
                exAgent.SetCurrentSheet(1); 
                FillInExcelData(dsSource, 3, 1);  
                exAgent.Save();
                exAgent.Close();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return filename;
        }
        #endregion
    }
}